<?php namespace App\Http\Controllers\Mobile;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Item;
use Illuminate\Support\Facades\Cache;

class IndexController extends Controller
{
    public function getIndex()
    {
        $equipment = Item::where('type', 'equipment')->take(4)->orderBy('created_at', 'DESC')->get();
        $news = Item::where('type', 'news')->take(3)->orderBy('created_at', 'DESC')->get();
        $case = Item::where('type', 'case')->take(4)->orderBy('created_at', 'DESC')->get();
        $link = Item::where('type', 'link')->take(4)->orderBy('created_at', 'DESC')->get();
        $about = Item::where('name', '公司简介')->first();
        $items = config('site.items');
        return view('mobile.index', [
            'equipment' => $equipment,
            'news' => $news,
            'link' => $link,
            'case' => $case,
            'items' => $items,
            'about' => $about,
            'pageName' => 'index'
        ]);
    }

    public function getAbout($id)
    {
        $model = Item::find($id);
        if ("" == $model || $model->type != "about") {
            abort(404);
        } else {
            return view('mobile.single', [
                'model' => $model,
                'data' => json_decode($model->content),
                'pageName' => 'about'
            ]);
        }
    }

    public function getService($id)
    {
        $model = Item::find($id);
        if ("" == $model || $model->type != "service") {
            abort(404);
        } else {
            return view('mobile.single', [
                'model' => $model,
                'data' => json_decode($model->content),
                'pageName' => 'service'
            ]);
        }
    }

    public function getKindcas($id = 0)
    {
        return view('mobile.case.index', [
            'id' => $id,
            'pageName' => 'kindcas'
        ]);
    }

    public function getKindcass($id = 0){
        $list = Item::where('type', 'case')->select('id','name','content');
        if (0 != $id) {
            $list = $list->where('kind', $id);
        }
        $list = $list->paginate(config('site.pageSize'));
        foreach($list as $key=>$item){
            $list[$key]['pic'] = json_decode($item->content)->pic;
            unset($list[$key]['content']);
        }
        return \Response::json($list);
    }

    public function getCase($id = 0)
    {
        $model = Item::find($id);
        if ("" == $model || $model->type != "case") {
            abort(404);
        } else {
            $model->type = 'kindcas';
            return view('mobile.single', [
                'model' => $model,
                'data' => json_decode($model->content),
                'pageName' => 'kindcas'
            ]);
        }
    }

    public function getKindeq($id = 0)
    {
        return view('mobile.kindeq.index', [
            'id' => $id,
            'pageName' => 'kindeq'
        ]);
    }

    public function getKindeqs($id = 0){
        $list = Item::where('type', 'equipment')->select('id','name','content');
        if (0 != $id) {
            $list = $list->where('kind', $id);
        }
        $list = $list->paginate(config('site.pageSize'));
        foreach($list as $key=>$item){
            $list[$key]['pic'] = json_decode($item->content)->pic;
            unset($list[$key]['content']);
        }
        return \Response::json($list);
    }

    public function getEquipment($id = 0)
    {
        $model = Item::find($id);
        if ("" == $model || $model->type != "equipment") {
            abort(404);
        } else {
            $model->type = 'kindeq';
            return view('mobile.single', [
                'model' => $model,
                'data' => json_decode($model->content),
                'pageName' => 'kindeq'
            ]);
        }
    }

    public function getContact()
    {
        $contact = Item::where('type', 'contact')->get();
        $items = config('site.items.contact');
        unset($items['data']['name']);
        return view('mobile.contact', [
            'contact' => $contact,
            'items' => $items,
            'pageName' => 'contact'
        ]);
    }

    public function getJoin()
    {
        $list = Item::where('type', 'join')->paginate(1);
        $items = config('site.items.join');
        unset($items['data']['content']);
        unset($items['data']['name']);
        return view('mobile.join.index', [
            'list' => $list,
            'items' => $items,
            'pageName' => 'join'
        ]);
    }

    public function getMap()
    {
        return view('mobile.map');
    }
}